set val(chan)           Channel/WirelessChannel    ;#Channel Type
set val(prop)           Propagation/TwoRayGround   ;# radio-propagation model
set val(netif)          Phy/WirelessPhy            ;# network interface type
set val(mac)            Mac/802_11                 ;# MAC type
set val(ifq)            Queue/DropTail		   ;# interface queue type
set val(ll)             LL                         ;# link layer type
set val(ant)            Antenna/OmniAntenna        ;# antenna model
set val(ifqlen)         50                        ;# max packet in ifq
set val(nn)             10                         ;# number of mobilenodes
set val(rp)             DumbAgent                  ;# routing protocol
set val(x)		600
set val(y)		600

Mac/802_11 set dataRate_ 11Mb

#Phy/WirelessPhy set CSThresh_ 10.00e-12
#Phy/WirelessPhy set RXThresh_ 10.00e-11
#Phy/WirelessPhy set Pt_ 0.1
#Phy/WirelessPhy set Pt_ 7.214e-3

# Initialize Global Variables
set ns_		[new Simulator]
set tracefd     [open infraCircCBR.tr w]
$ns_ trace-all $tracefd


# set up topography object
set topo       [new Topography]

$topo load_flatgrid $val(x) $val(y)

# Create God
create-god $val(nn)

# Create channel
set chan_1_ [new $val(chan)]


$ns_ node-config -adhocRouting $val(rp) \
		-llType $val(ll) \
		-macType $val(mac) \
		-ifqType $val(ifq) \
		-ifqLen $val(ifqlen) \
		-antType $val(ant) \
		-propType $val(prop) \
		-phyType $val(netif) \
		-topoInstance $topo \
		-agentTrace ON \
		-routerTrace ON \
		-macTrace ON \
		-movementTrace ON \
		-channel $chan_1_



      for {set i 0} {$i < 8} {incr i} {

                set node_($i) [$ns_ node]
		
                $node_($i) random-motion 0              ;# disable random motion
  		set mac_($i) [$node_($i) getMac 0]
 

      		$mac_($i) set RTSThreshold_ 3000
		
		#puts $i
		#puts [expr {10 + 5 * cos( $i * (2*asin(1))/4 ) }]
		#puts [expr {10 + 5 * sin( $i * (2*asin(1))/4 ) }]
		#puts "--"

		$node_($i) set X_ [expr {10 + 5 * cos( $i * (2*asin(1))/4 ) }]
  		$node_($i) set Y_ [expr {10 + 5 * sin( $i * (2*asin(1))/4 ) }]
  		$node_($i) set Z_ 0.0
		
	}

                set node_(8) [$ns_ node]
		
                $node_(8) random-motion 0              ;# disable random motion
  		set mac_(8) [$node_(8) getMac 0]
 

      		$mac_(8) set RTSThreshold_ 3000
		
		$node_(8) set X_ 10
  		$node_(8) set Y_ 10       ;# Horizontal arrangement of nodes
  		$node_(8) set Z_ 0.0

#Set Node 0 and Node $val(nn) as the APs. Thus the APs are the ends of the horizontal line. Each STA receives different power levels.


set AP_ADDR1 [$mac_(8) id]
$mac_(8) ap $AP_ADDR1
#set AP_ADDR2 [$mac_([expr $val(nn) - 1]) id]
#$mac_([expr $val(nn) - 1]) ap $AP_ADDR2

#$mac_([expr $val(nn) - 1]) set BeaconInterval_ 0.2


$mac_(1) ScanType ACTIVE

for {set i 3} {$i < [expr $val(nn) - 1]} {incr i} {
	$mac_($i) ScanType PASSIVE	;#Passive
}


$ns_ at 1.0 "$mac_(2) ScanType ACTIVE"

#Application/Traffic/CBR set packetSize_ 1023
#Application/Traffic/CBR set rate_ 256Kb

	
#for {set i 1} {$i < [expr $val(nn) - 1]} {incr i} {
#	set udp1($i) [new Agent/UDP]
#
#	$ns_ attach-agent $node_($i) $udp1($i)
#	set cbr1($i) [new Application/Traffic/CBR]
#	$cbr1($i) attach-agent $udp1($i)
#}


#set base0 [new Agent/Null]

#$ns_ attach-agent $node_(1) $base0

#set base1 [new Agent/Null]

#$ns_ attach-agent $node_(8) $base1

#$ns_ connect $udp1(4) $base0
#$ns_ connect $udp1(5) $base1

##

#Tráfego 1->5
#Cria um agente UDP e liga-o ao nó n0
set udp(1) [new Agent/UDP]
$ns_ attach-agent $node_(1) $udp(1)
set cbr(1) [new Application/Traffic/CBR]
$cbr(1) set packetSize_ 700
$cbr(1) set interval_ 0.00875
$cbr(1) attach-agent $udp(1)
#Cria um agente Null e liga-o ao nó n1
set null(1) [new Agent/Null]
$ns_ attach-agent $node_(5) $null(1)
$ns_ connect $udp(1) $null(1)

$ns_ at 2.0 "$cbr(1) start"


#Tráfego 5->1
#Cria um agente UDP e liga-o ao nó n0
set udp(2) [new Agent/UDP]
$ns_ attach-agent $node_(5) $udp(2)
set cbr(2) [new Application/Traffic/CBR]
$cbr(2) set packetSize_ 700
$cbr(2) set interval_ 0.00875
$cbr(2) attach-agent $udp(2)
#Cria um agente Null e liga-o ao nó n1
set null(2) [new Agent/Null]
$ns_ attach-agent $node_(1) $null(2)
$ns_ connect $udp(2) $null(2)

$ns_ at 2.0 "$cbr(2) start"




$ns_ at 20.0 "stop"
$ns_ at 20.0 "puts \"NS EXITING...\" ; $ns_ halt"

proc stop {} {
    global ns_ tracefd
    $ns_ flush-trace
    close $tracefd
    exit 0
}

puts "Starting Simulation..."
$ns_ run
